package model

import "errors"

var(
	ErrStockNotEnough = errors.New("库存不足")
)

type Book struct {
	Name string
	Total int
	Author string
	CreateTime string
}

func CreateBook(name string,total int,author string,createTime string) (b *Book){
	b = &Book{
		Name:name,
		Total:total,
		Author:author,
		CreateTime:createTime,
	}
	return
}

func (b *Book) canBorrow(c int) bool{
	return b.Total > c
}
//借书
func (b *Book) Borrow(c int)(err error){
	if !b.canBorrow(c) {
		err = ErrStockNotEnough
		return
	}
	b.Total -= c
	return nil
}
//还书
func (b *Book) Back(c int)  {
	b.Total += c
}